package love.yuanmengrobot.wuziqi.UIhht;

/*
 *   @Studio:CHJY · 源梦科技
 *   @Author:HuangHongTao
 *   @Ide:IntelliJ IDEA
 *   @Creation time:2021/5/10 10:52
 */

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.*;
import java.text.SimpleDateFormat;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
 * @author 1
 */
public class ChooseProp extends JFrame implements  Runnable{
    private String uid;


    private Date beginTime;
    Thread timer = new Thread(this);
    private int maxTime = 20;
    private int chooseTime = 20;
    private boolean buytime = true;


    public ChooseProp(String uid) {

        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
        Date date = new Date(System.currentTimeMillis());

         this.uid=uid;
         this.beginTime = beginTime;
         initComponents();
         timer.start();
    }

    private void initComponents() {
        this.addMouseListener(
                new MouseListener() {
                    @Override
                    public void mouseClicked(MouseEvent e) {

                    }

                    @Override
                    public void mousePressed(MouseEvent e) {

                    }

                    @Override
                    public void mouseReleased(MouseEvent e) {

                    }

                    @Override
                    public void mouseEntered(MouseEvent e) {

                    }

                    @Override
                    public void mouseExited(MouseEvent e) {

                    }
                }
        );
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        label1 = new JLabel();
        label2 = new JLabel();
        label3 = new JLabel();


        label4 = new JLabel();
        label5 = new JLabel();
        spinner1 = new JSpinner();
        spinner1.addChangeListener(
                new ChangeListener() {
                    @Override
                    public void stateChanged(ChangeEvent e) {
                        int num = Integer.parseInt(spinner1.getValue().toString());
                        if(num>3){
                            spinner1.setValue(3);
                        }else if(num<0){
                            spinner1.setValue(0);
                        }
                    }
                }
        );

        spinner2 = new JSpinner();
        spinner2.addChangeListener(
                new ChangeListener() {
                    @Override
                    public void stateChanged(ChangeEvent e) {
                        int num = Integer.parseInt(spinner2.getValue().toString());
                        if(num>3){
                            spinner2.setValue(3);
                        }else if(num<0){
                            spinner2.setValue(0);
                        }
                    }
                }
        );
        spinner3 = new JSpinner();
        spinner3.addChangeListener(
                new ChangeListener() {
                    @Override
                    public void stateChanged(ChangeEvent e) {
                        int num = Integer.parseInt(spinner3.getValue().toString());
                        if(num>3){
                            spinner3.setValue(3);
                        }else if(num<0){
                            spinner3.setValue(0);
                        }
                    }
                }
        );
        label6 = new JLabel();
        label7 = new JLabel();
        label8 = new JLabel();
        button1 = new JButton();
        button1.addMouseListener(
                new MouseListener() {
                    @Override
                    public void mouseClicked(MouseEvent e) {

                    }

                    @Override
                    public void mousePressed(MouseEvent e) {
                        int num1, num2, num3;

                        num1 = Integer.parseInt(spinner1.getValue().toString());
                        num2 = Integer.parseInt(spinner2.getValue().toString());
                        num3 = Integer.parseInt(spinner3.getValue().toString());
                      /*  System.out.println("杀棋："+num1);
                        System.out.println("变色棋："+num2);
                        System.out.println("炸棋："+num3);*/
                        if (buytime) {

                            String url = "jdbc:oracle:thin:@112.74.190.130:1521:orcl";
                            PreparedStatement stmt = null;//SQL语句对象
                            Connection conn = null;
                            String sql = "UPDATE wuziqiuserprops SET propnum=propnum-? WHERE userid= ? AND propid=?";
                            try {
                                Class.forName("oracle.jdbc.driver.OracleDriver");//
                            } catch (ClassNotFoundException ex) {
                                ex.printStackTrace();
                            }
                            try {
                                conn = DriverManager.getConnection(url, "scott", "scott1316");
                            } catch (SQLException ex) {
                                ex.printStackTrace();
                            }

                            if (num1 > 0 && num1 <= 3) {
                                if (QueryNum(1, num1)) {
                                    try {
                                        stmt = conn.prepareStatement(sql);
                                        stmt.setInt(1, num1);
                                        stmt.setString(2, uid);
                                        stmt.setString(3, "P01");
                                        stmt.executeUpdate();
                                    } catch (SQLException throwables) {
                                        throwables.printStackTrace();
                                    } finally {
                                        //释放资源：数据库连接很昂贵
                                        System.out.println("选择成功！");
                                        try {
                                            stmt.close();
                                            conn.close();//关连接
                                        } catch (SQLException throwables) {
                                            throwables.printStackTrace();
                                        }
                                    }

                                } else {
                                    System.out.println("选择失败：道具数量不足！");
                                }


                            }

                            if (num2 > 0 && num2 <= 3) {
                                if (QueryNum(2, num2)) {
                                    try {
                                        stmt = conn.prepareStatement(sql);
                                        stmt.setInt(1, num2);
                                        stmt.setString(2, uid);
                                        stmt.setString(3, "P02");
                                        stmt.executeUpdate();
                                    } catch (SQLException throwables) {
                                        throwables.printStackTrace();
                                    } finally {
                                        System.out.println("购买成功！");
                                        //释放资源：数据库连接很昂贵
                                        try {
                                            stmt.close();
                                            conn.close();//关连接
                                        } catch (SQLException throwables) {
                                            throwables.printStackTrace();
                                        }
                                    }
                                } else {
                                    System.out.println("购买失败：道具数量不足！");
                                }
                            }

                            if (num3 > 0 && num3 <= 3) {
                                if (QueryNum(3, num3)) {

                                    try {
                                        stmt = conn.prepareStatement(sql);
                                        stmt.setInt(1, num3);
                                        stmt.setString(2, uid);
                                        stmt.setString(3, "P03");
                                        stmt.executeUpdate();
                                    } catch (SQLException throwables) {
                                        throwables.printStackTrace();
                                    } finally {
                                        System.out.println("购买成功！");
                                        //释放资源：数据库连接很昂贵
                                        try {
                                            stmt.close();
                                            conn.close();//关连接
                                        } catch (SQLException throwables) {
                                            throwables.printStackTrace();
                                        }
                                    }
                                } else {
                                    System.out.println("购买失败：道具数量不足！");
                                }
                            }
                        }
                        else {
                            System.out.println("购买时间已结束！");
                        }
                    }








                    @Override
                    public void mouseReleased(MouseEvent e) {
                        spinner1.setValue(0);
                        spinner2.setValue(0);
                        spinner3.setValue(0);
                    }

                    @Override
                    public void mouseEntered(MouseEvent e) {

                    }

                    @Override
                    public void mouseExited(MouseEvent e) {

                    }
                }
        );
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        button2 = new JButton();
        button2.addMouseListener(
                new MouseListener() {
                    @Override
                    public void mouseClicked(MouseEvent e) {

                    }

                    @Override
                    public void mousePressed(MouseEvent e) {


                    }

                    @Override
                    public void mouseReleased(MouseEvent e) {

                    }

                    @Override
                    public void mouseEntered(MouseEvent e) {

                    }

                    @Override
                    public void mouseExited(MouseEvent e) {

                    }
                }
        );

        //======== this ========
        Container contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- label1 ----
        label1.setText("\u5373\u5c06\u5f00\u5c40\uff1a");
        label1.setFont(label1.getFont().deriveFont(label1.getFont().getSize() + 5f));
        contentPane.add(label1);
        label1.setBounds(new Rectangle(new Point(20, 20), label1.getPreferredSize()));

        //---- label2 ----
        label2.setText("\u4eba\u6570\uff1a");
        label2.setFont(label2.getFont().deriveFont(label2.getFont().getSize() + 5f));
        contentPane.add(label2);
        label2.setBounds(new Rectangle(new Point(20, 50), label2.getPreferredSize()));

        //---- label3 ----
        label3.setText("16s");



        label3.setFont(label3.getFont().deriveFont(label3.getFont().getSize() + 5f));
        contentPane.add(label3);
        label3.setBounds(new Rectangle(new Point(105, 20), label3.getPreferredSize()));

        //---- label4 ----
        label4.setText("3");
        label4.setFont(label4.getFont().deriveFont(label4.getFont().getSize() + 5f));
        contentPane.add(label4);
        label4.setBounds(75, 50, 20, label4.getPreferredSize().height);

        //---- label5 ----
        label5.setText("\u8bf7\u9009\u62e9\u9053\u5177");
        label5.setFont(label5.getFont().deriveFont(label5.getFont().getSize() + 8f));
        contentPane.add(label5);
        label5.setBounds(new Rectangle(new Point(290, 50), label5.getPreferredSize()));
        contentPane.add(spinner1);
        spinner1.setBounds(new Rectangle(new Point(145, 150), spinner1.getPreferredSize()));
        contentPane.add(spinner2);
        spinner2.setBounds(new Rectangle(new Point(295, 150), spinner2.getPreferredSize()));
        contentPane.add(spinner3);
        spinner3.setBounds(new Rectangle(new Point(435, 150), spinner3.getPreferredSize()));

        //---- label6 ----
        label6.setText("\u6740\u68cb");
        contentPane.add(label6);
        label6.setBounds(new Rectangle(new Point(120, 155), label6.getPreferredSize()));

        //---- label7 ----
        label7.setText("\u53d8\u8272\u68cb");
        contentPane.add(label7);
        label7.setBounds(new Rectangle(new Point(255, 155), label7.getPreferredSize()));

        //---- label8 ----
        label8.setText("\u70b8\u68cb");
        contentPane.add(label8);
        label8.setBounds(new Rectangle(new Point(405, 155), label8.getPreferredSize()));

        //---- button1 ----
        button1.setText("\u9009\u62e9");
        contentPane.add(button1);
        button1.setBounds(new Rectangle(new Point(215, 255), button1.getPreferredSize()));

        //---- button2 ----
        button2.setText("\u53d6\u6d88");
        contentPane.add(button2);
        button2.setBounds(new Rectangle(new Point(370, 255), button2.getPreferredSize()));

        contentPane.setPreferredSize(new Dimension(640, 395));
        pack();
        setLocationRelativeTo(getOwner());

        //---- label11 ----
        label11 = new JLabel(new ImageIcon("love/yuanmengrobot/wuziqi/Test/img/kill.jpg"));
        contentPane.add(label11);
        label11.setBounds(new Rectangle(new Point(170, 110), label11.getPreferredSize()));

        //---- label12 ----
        label12=new JLabel(new ImageIcon("love/yuanmengrobot/wuziqi/Test/img/change.jpg"));
        contentPane.add(label12);
        label12.setBounds(new Rectangle(new Point(320, 105), label12.getPreferredSize()));

        //---- label13 ----
        label13=new JLabel(new ImageIcon("love/yuanmengrobot/wuziqi/Test/img/boom.jpg"));
        contentPane.add(label13);
        label13.setBounds(new Rectangle(new Point(465, 105), label13.getPreferredSize()));

        contentPane.setPreferredSize(new Dimension(640, 395));
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents



    }




    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JLabel label1;
    private JLabel label2;
    private JLabel label3;
    private JLabel label4;
    private JLabel label5;
    private JSpinner spinner1;
    private JSpinner spinner2;
    private JSpinner spinner3;
    private JLabel label6;
    private JLabel label7;
    private JLabel label8;
    private JButton button1;
    private JButton button2;
    private JLabel label11;
    private JLabel label12;
    private JLabel label13;
    public boolean QueryNum(int num,int amount){
        int toamount = 0;
        String url = "jdbc:oracle:thin:@112.74.190.130:1521:orcl";
        PreparedStatement stmt = null;//SQL语句对象
        Connection conn = null;
        String sql = "SELECT propnum FROM wuziqiuserprops WHERE userid=? AND propid=?";
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");//
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }
        try {
            conn = DriverManager.getConnection(url, "scott", "scott1316");
        } catch (SQLException ex) {
            ex.printStackTrace();
        }

        try {
            stmt = conn.prepareStatement(sql);
            switch (num){
                case 1:stmt.setString(2,"P01");break;
                case 2:stmt.setString(2,"P02");break;
                case 3:stmt.setString(2,"P03");break;
                default:break;
            }
            stmt.setString(1, uid);
            ResultSet rs=null;


            rs= stmt.executeQuery();
            rs.next();
            toamount= Integer.parseInt(rs.getString(1));

        }  catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //释放资源：数据库连接很昂贵
            try {
                stmt.close();
                conn.close();//关连接
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (toamount>amount) return true;
        else return false;


    }





    public Date CheckTime() {
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
        Date date = new Date(System.currentTimeMillis());
        return date;
    }

    @Override
    public void run() {
        synchronized (timer) {
            if (maxTime > 0) {
                while (true) {
                    chooseTime--;
                    if (chooseTime == 0) {
                        JOptionPane.showMessageDialog(this, "购买时间结束!");
                        buytime =false;

                    }


                    label3.setText (String.valueOf(chooseTime)+"s");

                    this.repaint();
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }





    }

   /* int getPlayNum(){
        String url = "jdbc:oracle:thin:@112.74.190.130:1521:orcl";
        PreparedStatement stmt = null;//SQL语句对象
        Connection conn = null;
        String sql = "SELECT distinct  count (goods_id) from spec_goods_price";
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");//
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }
        try {
            conn = DriverManager.getConnection(url, "scott", "scott1316");
        } catch (SQLException ex) {
            ex.printStackTrace();
        }

                try {
                    stmt = conn.prepareStatement(sql);
                    stmt.setInt(1, num1);
                    stmt.setString(2, uid);
                    stmt.setString(3,"P01");
                    stmt.executeUpdate();
                }  catch (SQLException throwables) {
                    throwables.printStackTrace();
                } finally {
                    //释放资源：数据库连接很昂贵
                    System.out.println("购买成功！");
                    try {
                        stmt.close();
                        conn.close();//关连接
                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    }
                }
  return 1;
    }*/

    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
